package front;

import java.sql.Timestamp;
import java.util.Date;

import be.db_backend;

import mt.middle;

public class PM{

	private int pmId;
	private int from;
	private String fromEmail;
	private String toEmail;
	private int to;
	private Timestamp date;
	private String message;
	private middle m;
	
	public PM(int pmId, int from, int to, Timestamp date, String msg){
		this.pmId = pmId;
		this.from = from;
		this.to = to;
		this.date = date;
		this.message = msg;
		this.m = new middle(new db_backend());
		
		this.fromEmail = getEmailOfFrom();
		this.toEmail = getEmailOfTo();
		
	}
	
	public synchronized void Create(){
		try {
			int id = m.CreatePM(pmId, from, to, date, message);
			this.pmId = id;
		} catch (Exception e) {
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
	}
	
	public synchronized void Edit(){
		
	}
	
	public synchronized void Delete(){
		try {
			m.DeletePM(pmId);
			
		} catch (Exception e) {
			
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
		
	}
	
	public String getEmailOfFrom(){
		if(this.fromEmail == null)
			this.fromEmail = m.getEmail(from);
		if(this.fromEmail == "")
			this.fromEmail = m.getEmail(from);
		return this.fromEmail;
	}
	
	public String getEmailOfTo(){
		if(this.toEmail == null)
			this.toEmail = m.getEmail(to);
		if(this.toEmail == "")
			this.toEmail = m.getEmail(to);
		return this.toEmail;
	}
	
	public String ToString(){
		return "pmId:" + pmId +
				"from: " + from +
				", to: " + to +
				", msg: " + message +
				", date: " + date;
	}
	
	public int getFrom() {
		return from;
	}
	public void setFrom(int from) {
		this.from = from;
	}
	public int getTo() {
		return to;
	}
	public void setTo(int to) {
		this.to = to;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Timestamp date) {
		this.date = date;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public int getPmId() {
		return pmId;
	}

	public void setPmId(int pmId) {
		this.pmId = pmId;
	}

	public String getFromEmail() {
		return fromEmail;
	}

	public void setFromEmail(String fromEmail) {
		this.fromEmail = fromEmail;
	}

	public String getToEmail() {
		return toEmail;
	}

	public void setToEmail(String toEmail) {
		this.toEmail = toEmail;
	}
}
